package com.quik.web.front.mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.quik.backend.mapper.BaseDao;
import com.quik.common.Constants;
import com.quik.entity.BillingItem;
import com.quik.entity.DtaProduct;
import com.quik.entity.Statistics;
import com.quik.entity.DtaToken;
import com.quik.entity.DtaTokenType;
import com.quik.util.Tools;

/**
 * 功能说明：
 *     咨客手牌数据持久化类
 * @author jenny
 *
 */
@Repository
public class ConsultTokenDao extends BaseDao<Object> {
	/**
	 * 根据手牌类型或状态查询手牌
	 * @param tokenType
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public List findTokenList(Integer tokenType, String status, String section) throws Exception{
		Map map = new HashMap(3);
		map.put("tokenType", tokenType);
		map.put("status", status);
		map.put("section", section);
		
		List tokenList = this.load("findTokenListByStatusAndTokenType", map);
		return tokenList;
	}
	
	/**
	 * 查询手牌类型列表
	 * @return
	 * @throws Exception
	 */
	public List<DtaTokenType> findTokenTypeList() throws Exception{
		List<DtaTokenType> tokenTypeList = this.loadAll("findTokenTypeList");
		return tokenTypeList;
	}
	
	/**
	 * 查询手牌状态数量统计
	 * @param section
	 * @return
	 * @throws Exceptio
	 */
	public List<Statistics> findTokenStatList(String section) throws Exception{
		List<Statistics> tokenStatList = this.load("findTokenStatList", section);
		return tokenStatList;
	}
	
	/**
	 * 根据手牌、手牌类型、手牌状态查询手牌数据
	 * @param tokenNo
	 * @param tokenType
	 * @param status
	 * @param section
	 * @return
	 * @throws Exception
	 */
	public int findTokenCount(String tokenNo, Integer tokenType, String status, String section) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>(4);
		map.put("tokenNo", tokenNo);
		map.put("tokenType", tokenType);
		map.put("status", status);
		map.put("section", section);
		
		Integer count = (Integer)this.get("findTokenCountByStatusAndTokenType", map);
		return count;
	}
	
	/**
	 * 根据手牌、手牌类型、手牌状态查询手牌列表（分页）
	 * @param tokenNo
	 * @param tokenType
	 * @param status
	 * @param section
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List findTokenList(String tokenNo, Integer tokenType, String status, String section, Integer intPage, Integer pageSize) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>(6);
		map.put("tokenNo", tokenNo);
		map.put("tokenType", tokenType);
		map.put("status", status);
		map.put("section", section);
		map.put("currPage", (intPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		
		List tokenList = this.load("findTokenListByStatusAndTokenType", map);
		return tokenList;
	}
	
	/**
	 * 更新手牌状态
	 * @param tokenNo
	 * @param status
	 * @throws Exception
	 */
	public void updateTokenStatus(String tokenNo, String status, String user) throws Exception{
		Map<String, String> map = new HashMap<String, String>(3);
		map.put("tokenNo", tokenNo.trim());
		map.put("status", status);
		map.put("user", user);
		
		this.update("updateTokenStatus", map);
	}
	
	/**
	 * 手牌绑定的商品
	 * @param tokenNo
	 * @return
	 * @throws Exception
	 */
	public DtaProduct findTokenBoundProduct(String tokenNo) throws Exception{
		DtaProduct product = (DtaProduct)get("findTokenBoundProduct", tokenNo);
		return product;
	}
	
	/**
	 * 根据手牌号查询手牌明细项进行展示
	 * @param tokenNo
	 * @return
	 * @throws Exception
	 */
	public BillingItem findBillingItemByTokenNo(String tokenNo, String billingStatus) throws Exception{
		Map<String, String> map = new HashMap<String, String>(2);
		map.put("tokenNo", tokenNo);
		map.put("billingStatus", billingStatus);
		
		List billingItemList = this.load("findBillingItemByTokenNo", map);
		if(billingItemList != null && !billingItemList.isEmpty())
			return (BillingItem)billingItemList.get(0);
		else
			return null;
	}
	
	/**
	 * 根据手牌号查询手牌
	 * @param tokenNo 手牌号
	 * @return
	 * @throws Exception
	 */
	public DtaToken getToken(String tokenNo) throws Exception{
		DtaToken token = (DtaToken)get("getTokenByTokenNo", tokenNo);
		return token;
	}
	
	/**
	 * 根据手牌号查询手牌详情
	 * @param tokenNo
	 * @return
	 * @throws Exception
	 */
	public DtaToken getTokenByTokenNo(String tokenNo) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tokenNo", tokenNo);
		map.put("category", Tools.getSection(DtaToken.TABLE_NAME, Constants.SYSTEM_CODE_TOKENSTATUS));
		
		DtaToken token = (DtaToken)get("findTokenByTokenNo", map);
		return token;
	}
}
